﻿using Portfolio.DataModels;
using Portfolio.Web.IDataAdapters.Adapters;
using Portfolio.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Portfolio.Web.Controllers
{
    public class ProjectController : ApiController
    {
        IProjectAdapter _adapter = new ProjectAdapter();

        //Get api/item
        public IHttpActionResult Get()
        {
            List<Project> projects = _adapter.GetAllProjects().ToList();
            return Ok(projects);
        }

        //Get api/item/:id
        public IHttpActionResult Get(int id)
        {
            Project project = _adapter.GetProject(id);
            return Ok(project);
        }

        //Post api/item
        public IHttpActionResult Post(Project project)
        {
            project = _adapter.AddProject(project);
            return Ok(project);
        }

        //Put api/item
        public IHttpActionResult Put(Project project)
        {
            //if (User.IsInRole("Admin"))
            //{
                project = _adapter.UpdateProject(project);
                return Ok(project);
            //}
            //return Unauthorized();
        }

        //Delete api/item
        public IHttpActionResult Delete(int id)
        {
            if (User.IsInRole("Admin"))
            {
                Project project = _adapter.GetProject(id);
                if (project == null)
                {
                    return NotFound();
                }
                _adapter.DeleteProject(id);
                return Ok();
            }
            return Unauthorized();
        }
    }
}
